/**
 * 添加员工方法
 * @author    xiaogq
 * @param node
 */
function oper_add(node){
    var title = $(node).attr("title");
    var entityId = $(node).attr("tableCode");
    var tab = {id: "add"+entityId, text: "新建:"+title, url:"Employee/employeeAddInit.do?entityId="+entityId};
    parent.showTab(tab);
}

/**
 * 编辑员工方法
 * @author    xiaogq
 * @param node
 */
function fun_update(node){
    var id = $(node).attr("id");
    var title = $(node).attr("title");
    var entityId = $(node).attr("tableCode");
    var tab = {id: "edit"+entityId, text: "编辑:"+title, url:"Employee/employeeEditInit.do?entityId="+entityId+"&id="+ id};
    parent.showTab(tab);
}

/**
 * 查看员工详情方法
 * @author    xiaogq
 * @param node
 */
function fun_view(node){
    var id = $(node).attr("id");
    var title = $(node).attr("title");
    var rowName = $(node).attr("name");
    var entityId = $(node).attr("tableCode");
    var tab = {id: "view"+entityId, text: title, url:"Employee/employeeView.do?entityId="+entityId+"&id="+ id+"&rowName="+rowName+"&isself=false"};
    parent.showTab(tab);
}

/**
 * 删除员工方法
 * @author    xiaogq
 * @param node
 */
function fun_delete(node){
    //获取当前记录id
    var id = $(node).attr("id");
    var entityId = $(node).attr("tableCode");
    var url = "Employee/employeeDelete.do";
    //获取删除按钮的位置(showList:列表上，showColumn：列前，showView：详情页上)
    var showHere = $(node).attr("showHere");
    if(confirm("确定要删除该条数据吗？")){
        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: getRootPath()+"/"+ url +"?id="+id +"&entityId="+ entityId,
            dataType: 'json',
            success: function(data) {
                var flag = data.success;
                if(flag == "success"){
                    if(showHere == "showColumn"){
                        $("#bootstrapTable").bootstrapTable("refresh");
                    }else if(showHere == "showView"){
                        parent.closeTab(1);
                    }
                }else{
                    alert("管理员数据不能删除!");
                }
            },
            error: function(data) {
                alert("删除数据发生错误!");
            }
        });
    }
}

/**
 * 启用员工数据方法
 * @author    xiaogq
 * @param node
 */
function fun_enable(node) {
    //获取当前记录id
    var id = $(node).attr("id");
    var entityId = $(node).attr("tableCode");
    var url = "Employee/employeeEnable.do";
    if(confirm("确定要启用该条数据吗？")){
        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: getRootPath()+"/"+ url +"?id="+id +"&entityId="+ entityId,
            dataType: 'json',
            success: function(data) {
                $("#bootstrapTable").bootstrapTable("refresh");
            },
            error: function(data) {
                alert("启用数据发生错误!");
            }
        });
    }
}

/**
 * 停用员工数据方法
 * @author    xiaogq
 * @param node
 */
function fun_disable(node) {
    var id = $(node).attr("id");
    var entityId = $(node).attr("tableCode");
    var url = "Employee/employeeDisable.do";
    if(confirm("确定要停用该条数据吗？")){
        $.ajax({
            type: 'POST',
            contentType: 'application/json',
            url: getRootPath()+"/"+ url +"?id="+id +"&entityId="+ entityId,
            dataType: 'json',
            success: function(data) {
                $("#bootstrapTable").bootstrapTable("refresh");
            },
            error: function(data) {
                alert("停用数据发生错误!");
            }
        });
    }
}

/**
 * 清空密码方法
 * @author    xiaogq
 * @param node
 */
function fun_clearUppw(node){
    var id = $(node).attr("id");
    if(confirm("您确定清空密码？")){
        $.ajax({
            type:"POST",
            contentType: 'application/json',
            dataType: 'json',
            url:getRootPath()+"/Employee/emptyEmployeePassword.do?employeeId="+id,
            success: function(data){
                if(data.error){
                    alert(data.error);
                }else{
                    $("#bootstrapTable").bootstrapTable("refresh");
                }
            }
        });
    }
}

//修改密码
function oper_updatepwd(node){
    var recordId = $(node).attr("id");
    //获得主页面(frame.jsp)通用Modal弹窗模板
    var addModalNode = parent.document.getElementById("add_modal_content");
    var upwdHtml = getUpdatePwdDomInfo(recordId);
    $(addModalNode).empty();
    $(addModalNode).append(upwdHtml);
    parent.openAddModal();
}

/**
 * 拼接修改密码弹窗内容
 * @author   xiaogq
 */
function getUpdatePwdDomInfo(id){
    //弹窗标题部分
    var upwdHtml = "<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button><p class='modal-title'>修改登录密码</p></div>";
    //弹窗中间内容部分
    upwdHtml += "<div class='modal-body'><form class='form-horizontal' role='form'><ul>";
    upwdHtml += " <li class='from-li'><div class='fl from-li-name'><span>原密码:</span></div><div class='fl form-text'><input type='text' id='oldpwd' class='form-control'/></div></li>";
    upwdHtml += " <li class='from-li'><div class='fl from-li-name'><span>新密码:<span style='color: red'>*</span></span></div><div class='fl form-text'><input type='text' id='newpwd' class='form-control'/></div></li>";
    upwdHtml += " <li class='from-li'><div class='fl from-li-name'><span>确认新密码:<span style='color: red'>*</span></span></div><div class='fl form-text'><input type='text' id='confirmpwd' class='form-control'/></div></li>";
    upwdHtml += "</ul></form></div>";
    //弹窗按钮部分
    upwdHtml += "<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>取消</button><button type='button' class='btn btn-primary' recordId="+id+" onclick='saveUpdatePwd(this)'>保存</button></div>";
    return upwdHtml;
}

/**
 * 加入员工辅部门相关关系
 * @author   xiaogq
 * @param    empId    员工id
 * @param    deptIds  选中的部门id字符串
 */
window.parent.addEntityRelation = function (empId,deptIds) {
    if(empId != "" && deptIds != ""){
        $.ajax({
            type:"POST",
            contentType: 'application/json',
            dataType: 'json',
            url:getRootPath()+"/Dept/addEmpRelateDept.do?empId="+empId+"&deptIds="+deptIds,
            success: function(data){
                var flag = data.flag;
                if(!flag){
                    alert("添加员工相关辅部门失败!");
                }else{
                    $("#related_dept_table").bootstrapTable("refresh");
                }
            }
        });
    }
}

/**
 * 删除员工辅部门相关关系
 * @author   xiaogq
 */
function delEmpDeptRelation(node){
    var rid = $(node).attr("id");
    var type = $(node).attr("type");
    if(!type || type == "false"){
        if(confirm("您确要删除吗？")){
            $.ajax({
                type:"POST",
                contentType: 'application/json',
                dataType: 'json',
                url:getRootPath()+"/Dept/delEmpRelateDept.do?rId="+rid,
                success: function(data){
                    var flag = data.flag;
                    if(!flag){
                        alert("删除员工相关辅部门失败!");
                    }else{
                        $("#related_dept_table").bootstrapTable("refresh");
                    }
                }
            });
        }
    }else{
        alert("主部门不能删除!");
    }
}